package com.eshop.web.controllers.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.frameworks.core.entity.PageEntity;
import com.eshop.model.mongodb.EShop;
import com.eshop.service.mongodb.EShopService;

@Controller
@RequestMapping("/api")
public class EshopApiController extends BaseController{
	
	@Autowired
	EShopService eshopService;
	
	@RequestMapping(value="/getNearShop",method=RequestMethod.GET)
	@ResponseBody
	public String getNearShop(double lat,double lon,PageEntity page,HttpServletRequest request,HttpServletResponse response){
		if(page==null){
			page = new PageEntity();
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("lat", lat);
		jsonObject.put("lon", lon);
		jsonObject.put("currentPage", page.getCurrentPage());
		List<EShop> list = eshopService.searchShopps(lon, lat,page);
		jsonObject.put("resluts", list);
		jsonObject.put("success", true);
		jsonObject.put("totalResult", page.getTotalResultSize());
		jsonObject.put("totalPage", page.getTotalPageSize());
		jsonObject.put("currentPage", page.getCurrentPage());
		jsonObject.put("timeStamp", System.currentTimeMillis());
		return jsonObject.toJSONString();
	}
	
}
